<template>
  <div class="pad-20 bgc-default">
    <!-- 平台类型 -->
    <third-platform-head :showTmallIcon="false" :showKuaiShouIcon="false" @platformTypeChange="platformTypeChange">
    </third-platform-head>
    <div class="bg-color-white mar-t-15 pad-l-20 pad-r-20">
      <div class="mar-t-5">
        <template>
          <el-button type="primary" @click="doAddShop">添加店铺</el-button>
        </template>
      </div>
      <!--顶部搜索 -->
      <div class="top-operate-default mar-t-20">
        <div class="search-item">
          <label>搜索店铺：</label>
          <el-input style="width:200px;" v-model="searchListQuery.shopName" placeholder="请输入店铺名称"></el-input>
          <el-button-group class="mar-l-20">
            <el-button type="primary" @click="doSearch">查询</el-button>
            <el-button plain @click="doResetSearch">重置</el-button>
          </el-button-group>
        </div>
      </div>
      <!-- 内容列表 -->
      <div class="table-container mar-t-15">
        <el-table :data="dataList" v-loading="listLoading" element-loading-text="加载中">
          <el-table-column label="店铺名称" prop="shopName"></el-table-column>
          <el-table-column label="所属平台" prop="payNo">
            <template slot-scope="scope">
              <div>{{ dict.platformType[scope.row.platformType] }}</div>
            </template>
          </el-table-column>
          <el-table-column label="添加时间" prop="createTime"></el-table-column>
          <el-table-column label="操作" align="right" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" size="medium" @click="doEdit(scope.row)">编辑</el-button>
              <el-popconfirm title="您确定删除吗？" @onConfirm="doDelete(scope.row.thirdShopId)">
                <el-button slot="reference" class="mar-l-10" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 列表分页 -->
      <div class="pagination">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="searchListQuery.currentPage" :page-sizes="[10, 20, 50, 100]" :page-size="searchListQuery.size"
          layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </div>

    <!-- 店铺信息dialog -->
    <el-dialog title="店铺信息" :visible.sync="thirdShopVisible" width="500px">
      <el-form :model="thirdShopForm" ref="thirdShopForm" class="pad-10">
        <el-form-item label-width="120px" label="店铺所属平台：" prop="platformType">
          {{ dict.platformType[thirdShopForm.platformType] }}
        </el-form-item>
        <el-form-item label-width="120px" label="店铺ID：" prop="shopId">
          {{ thirdShopForm.shopId }}
        </el-form-item>
        <el-form-item label-width="120px" label="店铺名称：" prop="shopName">
          <span v-if="platformType == 3 || platformType == 5">{{ thirdShopForm.shopName }}</span>
          <el-input v-else class="width-300" v-model="thirdShopForm.shopName"></el-input>
        </el-form-item>
        <el-form-item label-width="120px" label="店铺网址：">
          <el-input class="width-300" v-model="thirdShopForm.shopUrl"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="thirdShopVisible = false">取消</el-button>
        <el-button type="primary" @click="doSave">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import thirdAuth from '.././components/third-auth'
  import thirdPlatformHead from '.././components/third-platform-head'
  import {
    pageThirdShop,
    saveThirdShop,
    deleteThirdShop
  } from '@/api/user/third-shop'

  export default {
    components: {
      thirdPlatformHead
    },
    mixins: [thirdAuth],
    data() {
      return {
        // 默认查询条件
        defaultQuery: {
          enterpriseId: '',
          supplierId: '',
          currentPage: 1,
          size: 10,
          shopName: ''
        },
        // 查询条件
        searchListQuery: {},
        // 第三方店铺表单
        thirdShopForm: {
          thirdShopId: '',
          shopId: '',
          shopName: '',
          shopType: '',
          shopUrl: '',
          enterpriseId: '',
          platformType: '',
          supplierId: ''
        },
        // 数据字典
        dict: {
          platformType: {}
        },
        // 店铺类型
        shopType: 3,
        // 平台类型
        platformType: 1,
        // 回调地址
        redirectUrl: '',
        total: 0,
        dataList: [],
        listLoading: false,
        thirdShopVisible: false
      }
    },
    // 初始化页面
    created() {
      this.searchListQuery = {
        ...this.defaultQuery
      }
      this.redirectUrl = '/#/shop/supplier/list'
      this.initList()
    },
    methods: {
      // 初始化
      initList() {
        this.searchListQuery.platformType = this.platformType
        this.searchListQuery.shopType = this.shopType
        this.searchListQuery.supplierId = this.getSupplierId()
        this.searchListQuery.enterpriseId = this.getEnterpriseId()
        this.listLoading = true
        pageThirdShop(this.searchListQuery).then(response => {
          this.listLoading = false
          if (response.isError) return
          this.total = response.data.total
          this.dataList = response.data.records
          this.dict = response.data.dict
        })
      },
      // 平台类型切换事件
      platformTypeChange(platformType) {
        this.platformType = platformType
        this.initList()
      },
      // 查询事件
      doSearch() {
        this.searchListQuery.currentPage = 1
        this.initList()
      },
      // 重置事件
      doResetSearch() {
        this.searchListQuery = {
          ...this.defaultQuery
        }
        this.initList()
      },
      // 添加店铺事件
      doAddShop() {
        const platformType = this.platformType * 1
        const redirectUrl = this.redirectUrl + '?platformType=' + platformType
        this.authByPlatform(platformType, redirectUrl)
      },
      // 编辑事件
      doEdit(row) {
        this.thirdShopVisible = true
        Object.assign(this.thirdShopForm, row)
      },
      // 保存事件
      doSave() {
        saveThirdShop(this.thirdShopForm).then(response => {
          if (response.isError) return
          this.$message({
            message: '保存成功！',
            type: 'success'
          })
          this.thirdShopVisible = false
          this.initList()
        })
      },
      // 删除事件
      doDelete(thirdShopId) {
        this.$confirm('删除店铺信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteThirdShop(thirdShopId).then(response => {
            if (response.isError) return
            this.$message({
              message: '删除成功！',
              type: 'success'
            })
            this.initList()
          })
        }).catch(() => {})
      },
      // 更改当前页数
      handleCurrentChange(val) {
        this.searchListQuery.currentPage = val
        this.initList()
      },
      // 更改当前页面大小
      handleSizeChange(val) {
        this.searchListQuery.size = val
        this.initList()
      }
    }
  }
</script>

<style lang="less" scoped>
  .user-avatar {
    width: 68px;
    height: 68px;
    display: block;
    padding: 1px;
    border: #efefef 1px solid;
  }
</style>
